package com.odm.communitymanage.ui.mine.EditData

import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.NetCallBack
import com.odm.communitymanage.core.bean.PostUpdateUserData
import com.odm.communitymanage.core.net.ApiService
import com.rxjava.rxlife.lifeOnMain
import jsc.kit.wheel.base.WheelItem
import jsc.kit.wheel.dialog.ColumnWheelDialog
import kotlinx.android.synthetic.main.fragment_edit_data.*
import rxhttp.wrapper.param.RxHttp

/**
 * @description: 编辑个人资料 页面
 * @author: ODM
 * @date: 2020/4/16
 */
class EditDataFragment  : BaseFragment() {


    override fun initViews() {

        //提交按钮的点击事件--> Post 请求更新用户信息
        btnUpdate_EditInfo?.setOnClickListener {
            val name = etName_EditInfo?.text.toString()
            val address = etAddress_EditInfo?.text.toString()
            val phone  = etPhone_EditInfo?.text.toString()
            val idCard = etIdCard_EditInfo?.text.toString()

            //请求提交信息
            RxHttp.postJson(ApiService.UPDATE_USER_INFO)
                .addAll(GsonUtils.toJson(PostUpdateUserData(idCard,name,phone,address)))
                .asObject(NetCallBack::class.java)
                .lifeOnMain(this)
                .subscribe({
                    if(it.code == 1){
                        updateRetainUserData(name ,phone, idCard)
                        showSuccessSnack(getString(R.string.post_success))
                    }else {
                        ToastUtils.showLong(it.message)
                    }
                },{
                    ToastUtils.showLong(it.message)
                    it.printStackTrace()
                })
        }
    }


    /**
     * 更新本地持久化的用户信息
     *
     * @param name String
     * @param phone String
     */
    private fun updateRetainUserData(name :String , phone :String ,idCardNumber :String){
        SPUtils.getInstance(Constants.SP_USER).put(Constants.USER_NAME , name)
        SPUtils.getInstance(Constants.SP_USER).put(Constants.USER_PHONE_NUMBER , phone)
        SPUtils.getInstance(Constants.SP_USER).put(Constants.USER_ID_CARD , idCardNumber)
    }



    override fun getLayoutId(): Int {
        return R.layout.fragment_edit_data
    }
}